﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace PFD
{
    class RegistryClass
    {
        #region IPersistentStorage Members

        static public object GetValue(string Storage, string Key, object DefaultValue)
        {
            object Value;
            RegistryKey rk;

            try
            {
                rk = Registry.CurrentUser.OpenSubKey(Storage, true);
            }
            catch (Exception)
            {
                rk = null;
            }

            if (rk == null)
            {
                rk = Registry.CurrentUser.CreateSubKey(Storage);
            }

            try
            {
                Value = rk.GetValue(Key, DefaultValue);
            }
            catch (Exception)
            {
                Value = null;
            }

            if (Value == null)
            {
                rk.SetValue(Key, DefaultValue);
                Value = rk.GetValue(Key, DefaultValue);
            }

            rk.Close();
            return Value;
        }

        static public void SetValue(string Storage, string Key, object Value)
        {
            RegistryKey rk;

            try
            {
                rk = Registry.CurrentUser.OpenSubKey(Storage, true);
            }
            catch (Exception)
            {
                rk = Registry.CurrentUser.CreateSubKey(Storage);
            }

            rk.SetValue(Key, Value);
        }

        #endregion
    }
}
